import unittest
from unittest import TestCase
from python_tools.flipper_file_to_ones_and_zeroes import flipper_file_to_run_length_lists, split_run_length_list


class Test(TestCase):
    def test_flipper_file_to_run_length_lists(self):
        expected_run_length_lists = [
            [442, 3115, 357, 1592, 518, 2934, 485, 1463, 204, 3800, 184],
            [197, 2623, 1197, 2807, 644, 6475, 1283, 772, 1328, 726, 646, 724, 1365, 2850, 654, 2824, 627, 2797, 707,
             1400, 626, 2826, 646, 1461, 618, 2912, 465, 1853, 258, 586, 205, 1401, 435],
            [184, 1818, 593, 775, 597, 1459, 620, 1461, 1336, 745, 1334, 2118, 1386, 668, 725, 724, 1313, 2798, 674,
             6441, 620, 749, 654, 716, 1363, 1404, 1414, 2774, 677, 2776, 675, 721, 672, 1408, 671, 1411, 678, 717, 644,
             1464, 647, 1381, 729, 693, 647, 1435, 654, 1426, 1360, 721, 1358, 2094, 1389, 719, 653, 691, 1388, 2827,
             645, 6416, 645, 751, 652, 744, 1335, 1404, 1361, 2829, 654, 2798, 674, 747, 646, 1436, 653, 1428, 651, 719,
             653, 1429, 650, 1431, 679, 769, 592, 1462, 627, 1427, 1359, 749, 1330, 2148, 1356, 725, 626, 796, 1283,
             2853, 598, 6544, 184, 1500, 204],
            [678, 666, 727, 2776, 696, 674, 698, 2780, 703, 1378, 701, 2776, 728, 1354, 704, 2747, 725, 1356, 723, 2755,
             728, 1353, 758, 2720, 700, 2093, 704, 1378, 701, 1380, 1385, 2777, 706, 6383, 699, 670, 702, 2750, 754,
             642, 730, 2748, 703, 1378, 701, 2776, 707, 1374, 705, 2773, 699, 1382, 697, 2754, 729, 1353, 726, 2751,
             700, 2092, 705, 1377, 702, 1379, 1407, 2755, 707, 6393, 679, 692, 701, 2776, 696, 674, 698, 2780, 703,
             1378, 701, 2777, 706, 1375, 704, 2774, 698, 1383, 706, 2744, 728, 1353, 726, 2778, 673, 2093, 704, 1377,
             702, 1380, 1385, 2777, 706, 6381, 680, 690, 703, 2748, 724, 672, 700, 2804, 679, 1376, 703, 2775, 697,
             1384, 705, 2772, 700, 1355, 724, 2753, 730, 1351, 728, 2750, 701, 2092, 705, 1376, 703, 1378, 1387, 2775,
             697, 6381, 680, 690, 703, 2748, 724, 698, 674, 2778, 705, 1376, 703, 2775, 697, 1384, 705, 2746, 726, 1381,
             698, 2753, 730, 1352, 727, 2750, 701, 2092, 705, 1376, 703, 1378, 1387, 2776, 696, 6391, 680, 690, 703,
             2748, 724, 672, 700, 2778, 705, 1376, 703, 2775, 729, 1352, 706, 2745, 727, 1380, 699, 2753, 730, 1351,
             728, 2750, 722, 2071, 704, 1376, 703, 1378, 1387, 2775, 729, 6360, 680, 690, 703, 2748, 724, 672, 700,
             2777, 706, 1376, 703, 2774, 698, 1384, 727, 2724, 727, 1380, 699, 2753, 730, 1351, 728, 2750, 701, 2065,
             732, 1349, 730, 1378, 1408, 2755, 696, 6392, 701, 642, 751, 2727, 724, 672, 700, 2778, 726, 1355, 703,
             2774, 698, 1384, 705, 2745, 759, 1322, 725, 2753, 730, 1351, 728, 2749, 702, 2065, 732, 1375, 704, 1378,
             1387, 2775, 697, 6400, 672, 698, 705, 2745, 759, 638, 702, 2774, 698, 1384, 727, 2751, 700, 1381, 698,
             2753, 730, 1351, 728, 2750, 754, 1327, 731, 2747, 704, 2088, 698, 1383, 706, 1375, 1390, 2773, 699, 6387,
             674, 696, 697, 2754, 729, 668, 704, 2773, 731, 1351, 696, 2781, 702, 1379, 700, 2751, 732, 1350, 729, 2748,
             724, 1357, 722, 2756, 706, 2086, 700, 1381, 698, 1383, 1382, 2781, 702, 6422, 649, 693, 700, 2778, 705,
             665, 706, 2798, 674, 1380, 699, 2779, 704, 1377, 702, 2750, 722, 1385, 726, 2725, 726, 1355, 724, 2754,
             697, 2069, 727, 1380, 699, 1382, 1383, 2780, 703, 6385, 676, 694, 699, 2752, 731, 666, 706, 2772, 700,
             1380, 699, 2779, 704, 1378, 701, 2750, 733, 1348, 731, 2747, 757, 1324, 723, 2754, 697, 2069, 728, 1380,
             699, 1382, 1383, 2780, 703, 6369, 671, 699, 725, 2752, 699, 670, 702, 2776, 696, 1385, 704, 2773, 699,
             1382, 697, 2754, 729, 1353, 726, 2751, 732, 1350, 729, 2748, 703, 2063, 734, 1374, 705, 1376, 1389, 2774,
             730, 6358, 703, 667, 705, 2746, 726, 670, 702, 2776, 707, 1374, 726, 2751, 700, 1382, 729, 2723, 728, 1379,
             700, 2751, 721, 1360, 729, 2748, 703, 2064, 733, 1374, 705, 1376, 1389, 2774, 698],
            [726, 1381, 698, 1383, 1382, 2781, 702, 6387, 705, 1349, 730, 667, 705, 665, 728, 668, 725, 2753, 698, 2068,
             729, 1352, 1413, 668, 725, 671, 701, 1381, 1416, 665, 728, 669, 703, 1404, 1382, 673, 1416, 2746, 705,
             2061, 725, 1383, 706, 1374, 1391, 2772, 700, 6388, 1380, 1360, 729, 1378, 701, 669, 703, 2774, 730, 2037,
             1414, 667, 726, 670, 702, 1379, 1418, 691, 670, 699, 704, 1377, 1409, 698, 674, 696, 697, 673, 730, 2747,
             704, 2062, 724, 1384, 705, 1376, 1389, 2773, 699, 6389, 672, 698, 705, 665, 728, 668, 704, 1404, 696, 2755,
             706, 1375, 704, 692, 1387, 694, 1385, 697, 706, 663, 1416, 665, 1414, 667, 726, 671, 1418, 662, 1417, 665,
             728, 2749, 702, 2064, 754, 1354, 704, 1377, 1388, 2775, 697, 6392, 1387, 667, 1412, 2066, 1385, 2778, 705,
             1376, 703, 1379, 731, 664, 697, 673, 1438, 1354, 704, 666, 758, 638, 1409, 1383, 706, 663, 730, 666, 1413,
             2750, 701, 2065, 732, 1376, 703, 1379, 1386, 2776, 707, 6385, 1383, 672, 1407, 2071, 1390, 2772, 700, 1381,
             698, 1383, 706, 690, 703, 693, 1386, 1380, 699, 671, 732, 664, 1436, 1356, 702, 668, 725, 671, 1408, 2755,
             696, 2097, 700, 1381, 698, 1383, 1382, 2781, 702, 6383, 678, 693, 731, 1375, 672, 1383, 1382, 2781, 723,
             1358, 700, 696, 728, 1353, 1391, 690, 703, 693, 700, 1355, 1410, 697, 727, 643, 729, 1352, 1444, 637, 724,
             2754, 697, 2095, 702, 1380, 699, 1382, 1383, 2780, 703, 6385, 676, 1379, 700, 1382, 697, 699, 1390, 2772,
             700, 2066, 731, 1350, 1415, 2063, 702, 1379, 1418, 2060, 705, 1376, 1410, 1356, 723, 2755, 728, 2038, 727,
             1381, 698, 1383, 1382, 2781, 702],
            [180, 17625, 180]
        ]
        actual = flipper_file_to_run_length_lists("test_flipper_file.ir")
        self.assertEqual(expected_run_length_lists, actual)

    def test_split_run_length_list_splits_on_large_run_of_zeroes(self):
        example_run_length_list = [694, 1388, 694, 9999, 694, 3470, 1388, 694, 694]
        expected_split_lists = [[694, 1388, 694], [694, 3470, 1388, 694, 694]]
        actual = split_run_length_list(example_run_length_list)
        self.assertEqual(actual, expected_split_lists)


    def test_split_run_length_list_ends_with_long_run_of_zeros_doesnt_return_empty_list(self):
        example_run_length_list = [694, 1388, 694, 9999, 694, 3470, 1388, 694, 694, 8000]
        expected_split_lists = [[694, 1388, 694], [694, 3470, 1388, 694, 694]]
        actual = split_run_length_list(example_run_length_list)
        self.assertEqual(actual, expected_split_lists)


if __name__ == '__main__':
    unittest.main()
